package com.crm.service.impl.trainingManager;

import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.customMapper.trainingCustomManager.TrainingCustomMapper;
import com.crm.dao.mapper.trainingManager.TrainingContactMapper;
import com.crm.dao.mapper.trainingManager.TrainingDemandItemMapper;
import com.crm.dao.mapper.trainingManager.TrainingDemandMapper;
import com.crm.dao.mapper.trainingManager.TrainingMapper;
import com.crm.model.entity.trainingManager.*;
import com.crm.model.qo.trainingManager.TrainingFormQO;
import com.crm.model.qo.trainingManager.TrainingQO;
import com.crm.model.vo.trainingManager.TrainingDetailVO;
import com.crm.model.vo.trainingManager.TrainingFormVO;
import com.crm.model.vo.trainingManager.TrainingVO;
import com.crm.service.trainingManager.TrainingDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 联系人管理信息
 */
@Service
public class TrainingDbServiceImpl implements TrainingDbService {
    @Autowired
    private TrainingCustomMapper trainingCustomMapper;

    @Autowired
    private TrainingMapper trainingMapper;

    @Autowired
    private TrainingDemandMapper trainingDemandMapper;

    @Autowired
    private TrainingDemandItemMapper trainingDemandItemMapper;

    @Autowired
    private TrainingContactMapper trainingContactMapper;

    /**
     * 用户模块 -- 内训管理列表
     * @param trainingQO 查询条件
     * @param dateUnit 1上月 2本月
     * @param sortOrder 分组
     * @return
     */
    @Override
    public List<TrainingVO> selectTrainingList(TrainingQO trainingQO, Integer dateUnit, String sortOrder) {
        return trainingCustomMapper.selectTrainingList(trainingQO,dateUnit,sortOrder);
    }

    /**
     * 新增内训管理
     * @param tra 内训实体
     * @return
     */
    @Override
    public Boolean addTraining(Training tra) {
        Boolean result = false;
        int num = trainingMapper.insertSelective(tra);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 内训详情-通过内训id查询内训详情
     * @param trainingId 内训id
     * @return
     */
    @Override
    public TrainingVO selectByTrainingId(String trainingId) {
        return trainingCustomMapper.selectByTrainingId(trainingId);
    }

    /**
     * 内训管理-新增内训
     * @param trainingDemand 新增内训需求实体
     * @return
     */
    @Override
    public Boolean addTrainingDemand(TrainingDemand trainingDemand) {
        Boolean result = false;
        int num = trainingDemandMapper.insertSelective(trainingDemand);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 内训管理-修改内训需求
     * @param trainingDemand 新增内训需求实体
     * @return
     */
    @Override
    public Boolean updateTrainingDemand(TrainingDemand trainingDemand) {
        Boolean result = false;
        int num = trainingDemandMapper.updateByPrimaryKeySelective(trainingDemand);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 用户模块 -- 内训管理信息
     * @param trainingId 内训线索id
     * @return
     */
    @Override
    public TrainingDemand selectTrainingDemand(String trainingId) {
        TrainingDemandExample trainingDemandExample = new TrainingDemandExample();
        TrainingDemandExample.Criteria criteria = trainingDemandExample.createCriteria();
        criteria.andTrainingIdEqualTo(trainingId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<TrainingDemand> list = trainingDemandMapper.selectByExample(trainingDemandExample);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return new TrainingDemand();
    }

    /**
     * 内训管理-新增内训需求项
     * @param trainingDemandItem 新增内训需求项实体
     * @return
     */
    @Override
    public Boolean addTrainingDemandItem(TrainingDemandItem trainingDemandItem) {
        Boolean result = false;
        int num = trainingDemandItemMapper.insertSelective(trainingDemandItem);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 内训详情-内训管理项列表
     * @param sortOrder 排序字段
     * @param trainingId 内训线索id
     * @return
     */
    @Override
    public List<TrainingDemandItem> selectTrainingDemandItemList(String trainingId, String sortOrder) {
        TrainingDemandItemExample trainingDemandItemExample = new TrainingDemandItemExample();
        TrainingDemandItemExample.Criteria criteria = trainingDemandItemExample.createCriteria();
        trainingDemandItemExample.setOrderByClause(sortOrder);
        criteria.andTrainingIdEqualTo(trainingId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<TrainingDemandItem> list = trainingDemandItemMapper.selectByExample(trainingDemandItemExample);
        return list;
    }

    /**
     * 通过用户id查询内训信息
     * @param accountId 用户id
     * @return
     */
    @Override
    public List<Training> trainingList(String accountId) {
        TrainingExample trainingExample = new TrainingExample();
        TrainingExample.Criteria criteria = trainingExample.createCriteria();
        criteria.andAccountIdEqualTo(accountId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<Training> list = trainingMapper.selectByExample(trainingExample);
        return list;
    }

    /**
     * 内训详情-内训联系人
     * @param sortOrder 排序字段
     * @param trainingId 内训线索id
     * @return
     */
    @Override
    public List<TrainingContact> selectTrainingContactList(String trainingId, String sortOrder) {
        TrainingContactExample trainingContactExample = new TrainingContactExample();
        TrainingContactExample.Criteria criteria = trainingContactExample.createCriteria();
        trainingContactExample.setOrderByClause(sortOrder);
        criteria.andTrainingIdEqualTo(trainingId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        //criteria.andAbolishStateEqualTo(1);
        List<TrainingContact> list = trainingContactMapper.selectByExample(trainingContactExample);
        return list;
    }

    /**
     * 查询同一个内训下联系人只能有一个首要联系人
     * @param trainingId 内训id
     * @param contactState 联系类型 主联系人-1   从联系人-2
     * @return
     */
    @Override
    public List<TrainingContact> getTrainingAndContactState(String trainingId, Integer contactState) {
        TrainingContactExample trainingContactExample = new TrainingContactExample();
        TrainingContactExample.Criteria criteria = trainingContactExample.createCriteria();
        criteria.andTrainingIdEqualTo(trainingId);
        criteria.andContactStateEqualTo(contactState);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        //criteria.andAbolishStateEqualTo(1);
        List<TrainingContact> list = trainingContactMapper.selectByExample(trainingContactExample);
        return list;
    }

    /**
     * 修改内训联系人
     * @param contact 联系人
     * @return
     */
    @Override
    public Boolean updateTrainingContact(TrainingContact contact) {
        Boolean result = false;
        int num = trainingContactMapper.updateByPrimaryKeySelective(contact);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 新增内训联系人
     * @param trainingContact 联系人
     * @return
     */
    @Override
    public Boolean addTrainingContact(TrainingContact trainingContact) {
        Boolean result = false;
        int num = trainingContactMapper.insertSelective(trainingContact);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 用户模块 -- 内训详情 -- 同一内训线索下内训联系人的联系方式不能相同
     * @param trainingId 内训线索id
     * @param contactInformation 联系方式
     * @return
     */
    @Override
    public TrainingContact getTrainingIdAndContactInformation(String trainingId, String contactInformation) {
        TrainingContactExample trainingContactExample = new TrainingContactExample();
        TrainingContactExample.Criteria criteria = trainingContactExample.createCriteria();
        criteria.andTrainingIdEqualTo(trainingId);
        criteria.andContactInformationEqualTo(contactInformation);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        //criteria.andAbolishStateEqualTo(1);
        List<TrainingContact> list = trainingContactMapper.selectByExample(trainingContactExample);
        if(list != null && list.size() > 0) {
            return list.get(0);
        }else {
            return new TrainingContact();
        }

    }

    /**
     * 用户模块 -- 内训详情 -- 修改联系人回显
     * @param trainingContactId 联系人id
     * @return
     */
    @Override
    public TrainingContact getTrainingContact(String trainingContactId) {
        return trainingContactMapper.selectByPrimaryKey(trainingContactId);
    }


    /**
     * 管理模块--内训线索池列表
     * @param trainingFormQO --查询条件
     * @param dateUnit       --上月、本周、本月
     * @param sortOrder      --排序
     * @author yincuimei
     * @return
     */
    @Override
    public List<TrainingFormVO> selectTrainingFormVOList(TrainingFormQO trainingFormQO, Integer dateUnit, String sortOrder) {
        return trainingCustomMapper.selectTrainingFormVOList(trainingFormQO,dateUnit,sortOrder);
    }

    /**
     * 管理模块-》内训线索池列表-》内训详情
     * @param trainingId  --内训ID
     * @author yincuimei
     * @return
     */
    @Override
    public TrainingDetailVO selectTrainingDetailVOById(String trainingId) {
        return trainingCustomMapper.selectTrainingDetailVOById(trainingId);
    }

    /**
     * 管理模块-》内训线索池列表-》内训详情-》编辑内训
     * @param training
     * @author yincuimei
     * @return
     */
    @Override
    public Boolean updateTrainingById(Training training) {
        return trainingMapper.updateByPrimaryKeySelective(training) > 0;
    }

    /**
     * @Author renshaorong
     * @Date 2021/9/24
     * @param trainingId  --内训ID
     * @return
     */
    @Override
    public Training selectByPrimaryKey(String trainingId) {
        return trainingMapper.selectByPrimaryKey(trainingId);
    }
}
